﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_ccEnergyAnalysis_Default : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            AcmsContext.EnsureLoggedIn(this);
            if (AcmsContext.CurrentPost != null)
            {
                lnkAuditOne.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../EnergyAudit?Audit=1");
                lnkAuditTwo.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../EnergyAudit?Audit=2");
                lnkPupilNumbers.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../");
                lnkWasteAudit.NavigateUrl = AcmsContext.CurrentPost.ResolvePath("../WasteAudit?Audit=1");
            }

            DataBind();
        }
    }

    public override void DataBind()
    {
        bool hasPupilNumbersEntered = false;
        ClimateChangeChampionsData.DataContext context = new ClimateChangeChampionsData.DataContext();
        var audits = ClimateChangeChampionsData.CarbonCalculation.GetAllByUserId(context, User.Identity.User.Id);

        if (audits.Count >= 1 && audits.First().NoPupils > 0)
        {
            hasPupilNumbersEntered = true;
        }

        if (!hasPupilNumbersEntered)
        {
            Response.Redirect(AcmsContext.CurrentPost.Parent.Url);
        }
        else if (audits.Count != 2 || audits.First().KWH == null)
        {
            Response.Redirect(AcmsContext.CurrentPost.ResolvePath("../EnergyAudit?Audit=1"));
        }
        else if(audits.Skip(1).First().KWH == null)
        {
            Response.Redirect(AcmsContext.CurrentPost.ResolvePath("../EnergyAudit?Audit=2"));
        }
        else
        {
            var first = audits.ElementAt(0);
            var second = audits.ElementAt(1);
            bool saving = false;
            decimal difference = ((decimal)second.KWH.Value - (decimal)first.KWH.Value) / (decimal)first.KWH.Value;
            var differenceKGCO2PerWeek = second.KGCO2PerWeek - first.KGCO2PerWeek;
            var differenceTonnes = second.TonnesCO2PerSchoolYear - first.TonnesCO2PerSchoolYear;
            if (difference < 0)
            {
                saving = true;
                difference = difference * -1;
                differenceKGCO2PerWeek = differenceKGCO2PerWeek * -1;
                differenceTonnes = differenceTonnes * -1;
            }


            //format percentage to two decimal places
            lblPercent.Text = string.Format("{0:0.##%} {1}", difference, saving ? "less" : "more");
            //show difference per week to two decimal places
            lblKGCO2Wk.Text = string.Format("{0} {1:0.##}", saving ? "Saving" : "An extra", differenceKGCO2PerWeek);
            //show difference per week as party balloons to zero decimal places
            lblIcKGCO2Wk.Text = (differenceKGCO2PerWeek * 1306).ToString("0");
            //show difference per year to two decimal places
            lblTonCO2Yr.Text = string.Format("{0} {1:0.##}", saving ? "Saving" : "An extra", differenceTonnes);
            //show difference per year as planes to zero decimal places
            lblIcTonCO2Yr.Text = (differenceTonnes * 1.225).ToString("0");

            flash.Variables["Audit1"] = first.KGCO2PerWeek.ToString();
            flash.Variables["Audit2"] = second.KGCO2PerWeek.ToString();
        }
    }
}


